<template>
  <div class="login-container">
    <div class="left">
      <div class="title-box">
        <img src="@/assets/img/login_icon.png" alt="" />
        <div class="title">黑马面面</div>
        <div class="line"></div>
        <div class="sub-title">用户登录</div>
      </div>

      <div class="form">
        <el-form
          :model="form"
          ref="form"
          :rules="rules"
          :inline="false"
          size="normal"
          class="login-form"
        >
          <el-form-item prop="phone">
            <el-input
              v-model="form.phone"
              placeholder="请输入手机号码"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="form.password"
              placeholder="请输入密码"
            ></el-input>
          </el-form-item>
          <el-row>
            <el-col :span="16">
              <el-form-item prop="code">
                <el-input
                  v-model="form.code"
                  placeholder="请输入验证码"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <img
                class="captcha"
                :src="codePic"
                alt=""
                @click="codePicClick"
              />
            </el-col>
          </el-row>
          <el-form-item prop="checked">
            <el-checkbox v-model="form.checked"></el-checkbox>
            我阅读并同意协议
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width:100%" @click="login"
              >登录</el-button
            >
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              style="width:100%"
              @click="$refs.registRef.dialogVisible = true"
              >注册</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="right">
      <img src="@/assets/img/login_bg.png" alt="" />
    </div>
    <regist ref="registRef" />
  </div>
</template>
<script>
import regist from './regist'
import { saveToken } from '../../utile/token'
export default {
  name: 'userLogin',
  components: {
    regist
  },
  data () {
    return {
      codePic: process.env.VUE_APP_BASEURL + '/captcha?type=login',
      form: {
        phone: '18511111111',
        password: '12345678',
        checked: true,
        code: ''
      },
      rules: {
        phone: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(new Error('请输入手机号码'))
              }
              const reg = /^1[3-9][0-9]{9}$/
              if (!reg.test(value)) {
                return callback(new Error('请输入合法的手机号'))
              }
              callback()
            },
            trigger: 'blur'
          }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          {
            min: 4,
            max: 4,
            message: '验证码必须为四位有效数字',
            trigger: 'blur'
          }
        ],
        checked: [
          {
            required: true,
            validator: (rule, value, callback) => {
              value ? callback() : callback(new Error('请必须勾选协议'))
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    codePicClick () {
      this.codePic =
        process.env.VUE_APP_BASEURL + '/captcha?type=login&sb=' + Date.now()
    },
    login () {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        const res = await this.$axios.post('/login', this.form)
        if (res.code === 200) {
          this.$message({
            type: 'success',
            message: '登录成功',
            duration: 1000
          })
          // localStorage.getItem('heima', res.data.data.token)
          // console.log(res.data.data.token)
          saveToken(res.data.token)
          this.$router.push('/layout')
        } else {
          this.$message({
            type: 'error',
            message: '登录失败,请检查信息后重新登录',
            duration: 1000
          })
          this.codePicClick()
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login-container {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-around;
  background: linear-gradient(225deg, #1493fa, #01c6fa);
  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 48px;
    .title-box {
      display: flex;
      align-items: center;
      img {
        width: 22px;
        height: 17px;
        margin-right: 16px;
      }
      .title {
        font-size: 24px;
        margin-right: 14px;
      }
      .line {
        width: 1px;
        background-color: #c7c7c7;
        height: 27px;
        margin-right: 12px;
      }
      .sub-title {
        font-size: 22px;
      }
    }
    .login-form {
      margin-top: 29px;
      .captcha {
        width: 100%;
        height: 40px;
        margin-left: 10px;
      }
    }
  }
}
</style>
